import configparser,paramiko,ibm_db_dbi
 
#解析config文件，返回配置对应的字符串或数字
def myconfig(section,env,type='isstr'):
   config = configparser.ConfigParser()
   config.read('config.ini')
   if type=='isint':
       return config.getint(section, env)
   else:
       return config.get(section,env)
 
# 定义连接远程服务器的方法，默认只能连接126，其他ftp需要在config中配置
def mySftp(ftpaddr='126'):
   hostname = myconfig(ftpaddr, 'host')
   port = myconfig(ftpaddr, 'port','isint')
   username = myconfig(ftpaddr, 'username')
   password = myconfig(ftpaddr, 'password')
   t = paramiko.Transport(hostname, port)
   t.connect(username=username, password=password)
   sftp = paramiko.SFTPClient.from_transport(t)
   return sftp
 
# 解析数据库配置，建立连接的类
class DBHelper(object):
   def __init__(self, conn):
       temp = ''
       for val in conn.keys():
           temp = temp + val + '=' + conn[val] + ';'
       self.__conn = str.format(temp)
 
   def queryAction(self, sql=None):
       temp = []
       try:
           conn = ibm_db_dbi.connect(self.__conn, "", "")
       except Exception as e:
           print(u'连接DB2数据库失败')
       else:
           print('执行sql为：%s' % sql)
           cur = conn.cursor()
           cur.execute(sql)
           temp = cur.fetchall()
       return temp